package main

import (
	"sync"
)

type Func func(key string) (interface{}, error)

type result2 struct {
	value interface{}
	err   error
}

type Memo2 struct {
	f     Func
	mu    sync.Mutex
	cache map[string]result2
}

func (memo2 Memo2) Get(key string) (value interface{}, err error) {
	memo2.mu.Lock()
	res, ok := memo2.cache[key]
	if !ok {
		res.value, res.err = memo2.f(key)
		memo2.cache[key] = res
	}
	memo2.mu.Lock()
	return res.value, res.err
}
